package me.leiusual.sharing.repository

import androidx.lifecycle.LiveData
import androidx.lifecycle.Transformations.switchMap
import androidx.paging.Config
import androidx.paging.PagedList
import androidx.paging.toLiveData
import io.reactivex.Single
import me.leiusual.sharing.api.ApiSharing
import me.leiusual.sharing.api.model.UserMessage
import me.leiusual.sharing.repository.datasource.UserMessageDataSourceFactory
import me.leiusual.sharing.repository.model.Data
import javax.inject.Inject
import javax.inject.Singleton

/**
 * author: leiusual
 * create on: 2019-08-04
 * description:
 */
@Singleton
class UserMessageRepository @Inject constructor(
    private val apiSharing: ApiSharing
) {

  fun userMessageQuery(): UserMessageQueryListing {
    val userMessageDataSourceFactory = UserMessageDataSourceFactory(apiSharing)
    val loadInitial = switchMap(userMessageDataSourceFactory.userMessageDataSource) { it.loadInitial }
    val pagedList = userMessageDataSourceFactory.toLiveData(Config(9, initialLoadSizeHint = 9))
    return UserMessageQueryListing(loadInitial, pagedList)
  }

  data class UserMessageQueryListing(
      val loadInitial: LiveData<Data<Unit>>,
      val pagedList: LiveData<PagedList<UserMessage>>
  )

  fun userMessageView(id: Long): Single<Unit> {
    return apiSharing.userMessageView(id)
  }
}